<?php
namespace App\Admin\Controllers\Garage;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use App\Models\Garage\Vehicle as ThisModel;

class VehicleController extends AdminController
{
    protected $title = '汽车管理';

    protected function grid()
    {
        $presents = ThisModel::getPresents();
        $types = ThisModel::getTypes();

        $grid = new Grid(new ThisModel);

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('ID'))->sortable();
        $grid->column('type', __('Type'))->using($types);
        $grid->column('model', __('Model'));
        $grid->column('plate_number', __('Plate number'));
        $grid->column('chassis_number', __('Chassis number'));

        $grid->column('key_present', __('Key'))->using($presents);
        $grid->column('registration_present', __('Registration'))->using($presents);
        $grid->column('door', __('Door'))->using($presents);
        $grid->column('items', __('Items'))->using($presents);
        $grid->column('damage', __('Damage'))->using($presents);
        $grid->column('seal', __('Seal'))->using($presents);

        $grid->column('created_by', __('Created by'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->filter(function ($filter) use ($types) {
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) use ($types) {
                $filter->equal('id', __('ID'));
                $filter->equal('type', __('Type'))->select($types);
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('model', __('Model'));
                $filter->equal('plate_number', __('Plate number'));
                $filter->equal('chassis_number', __('Chassis number'));
            });
            $filter->column(1/3, function ($filter) {
                $filter->between('created_at', __('Created at'))->date();
                $filter->between('updated_at', __('Updated at'))->date();
            });
        });

        return $grid;
    }
    protected function detail($id)
    {
        $show = new Show(ThisModel::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }
    protected function form()
    {
        $form = new Form(new ThisModel);

        $form->display('id', __('ID'));
        // $form->display('created_at', __('Created At'));
        // $form->display('updated_at', __('Updated At'));

        $form->disableViewCheck()
            ->disableCreatingCheck();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });


        return $form;
    }
}
